﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace Sequence
{
    public partial class FrmQueryDef : Form
    {
        public OleDbConnection m_connection;
        public string sID;
        private string sSql;

        public FrmQueryDef()
        {
            InitializeComponent();
        }

        public FrmQueryDef(string sType)
        {
            InitializeComponent();
            switch (sType)
            {
                case "QueryDef":
                    this.Text = "QueryDef";
                    sSql = Sequence.CSqlString.GetQuerydef("OCI", textBox1.Text.ToLower());
                    break;
                case "QueryDefProcess":
                    this.Text = "QueryDef Process";
                    sSql = Sequence.CSqlString.GetQuerydefProcess("OCI", textBox1.Text.ToLower());
                    break;

            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {            
            try
            {
                OleDbCommand command = new OleDbCommand(sSql, m_connection);
                OleDbDataReader oDataReader = command.ExecuteReader();
                lvQueries.Items.Clear();
                while (oDataReader.Read())
                {
                    ListViewItem oItem = lvQueries.Items.Add(oDataReader[0].ToString());
                    oItem.SubItems.Add(oDataReader[1].ToString());
                }
                oDataReader.Close();
                oDataReader.Dispose();
                command.Dispose();

            }
            catch (Exception ex)
            {
                //WriteError(ex.Message + " (" + sSql + ")");
            }
        }

        private void FrmQueryDef_Load(object sender, EventArgs e)
        {

        }

        private void cmdOpen_Click(object sender, EventArgs e)
        {
            cmdOpenClick();            
        }

        private void cmdOpenClick()
        {
            if (lvQueries.SelectedItems.Count > 0)
            {
                ListView.SelectedListViewItemCollection items = lvQueries.SelectedItems;
                ListViewItem lvItem = items[0];
                sID = lvItem.SubItems[1].Text;
            }
            this.Close();
        }

        private void lvQueries_DoubleClick(object sender, EventArgs e)
        {
            cmdOpenClick();            
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            sID = "";
            this.Close();
        }
    }
}
